<?php

namespace App\Http\Controllers\Home;

use Illuminate\Http\Request;
use DB;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Session;
use App\Model\Home\Login;

class UserController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $user = Session::get('user')[0];
        //dd($user['img']);
        return view('home.user_index',['user'=>$user]);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function logout()
    {
       Session::forget('user');
        return redirect('home/login');

    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //

        return view('home.user_edit',['id'=>$id]);

    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        if($request->hasFile('img')){
            $file = $request->file('img');
            if($file->isValid()){
                $photo = $file->getClientOriginalName();
                $ext = $file->getClientOriginalExtension();
                $photo_path = "images/user/";
                $filename = time().rand(1000,9999).'.'.$ext;
                $file->move($photo_path,$filename);
                $img = "/".$photo_path.$filename;
            }
        }  
       // dd($img);
        $user = Session::pull('user')[0];
        //$path = "E:\xampp\htdocs\bookshop\public";
        $path = 'public/'.ltrim($user['img'],'/');

        if(isset($img)){
            unlink(base_path($path));
        }
        
        $data['nickname'] = $request->nickname ? $request->nickname : $user['nickname'];
        $data['password'] = $request->add ? md5($request->password) : $user['password'];
        $data['img']      = isset($img) ? $img : $user['img'];
        $data['sex']      = $request->sex ? $request->sex : 1;
        $data['phone']    = $request->phone ? $request->phone : $user['phone'];
        $data['email']    = $request->email ? $request->email : $user['email'];
        $data['add']      = $request->add ? $request->add : $user['add'];
        DB::table('user')->where('id',$id)->update($data);
        //设置Session里面的信息



        $user1 = Login::where('id',$id)->first();
        //dd($user1['original']);



        Session::push('user',$user1['original']);
        return redirect('home/user');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }


    //
    public function order($id)
    {
        $user = Session::get('user')[0];
        //dd($user['id']);
        $data = DB::table('orders')->where('user_id',$user['id'])->get();
        //dd($data);
        //获取商品信息
        $goods = DB::table('orders_info')
                ->join('goods','goods.id','=','orders_info.goods_id')
                ->select('goods.*','orders_info.num','orders_info.orders_id')
                ->get();
        //dd($goods);
        return view('home.user_order',['user'=>$user,'order'=>$data,'goods'=>$goods]);
    }


    //订单删除
    public function delorder($id)
    {
        DB::table('orders')->where('id',$id)->delete();
        $user = Session::get('user')[0];
        return redirect('home/order/$id');
    }




    //用户评论页面
    public function comment($id)
    {   
        $user = Session::get('user')[0];
        $goods =  DB::table('goods')->where('id',$id)->first();
        return view('home.comment_add',['goods'=>$goods,'user'=>$user]);
    }
    //用户评论添加
    public function addcomment(Request $request,$id)
    {
        //dd($request->comment);
        $data['user_id']    =  Session::get('user')[0]['id'];
        $data['goods_id']   =  $id;
        $data['content']    =  $request->comment;
        $data['time']       =  time();
        //添加用户评论
        DB::table('comment')->insertGetId($data);
        return redirect('/home/listcomment');

    }
    //用户所有评论页面
    public function listcomment()
    {
       /* echo 1111;
        die;*/
        $user = Session::get('user')[0];
        $data = DB::table('goods')
            ->join('comment','comment.goods_id','=','goods.id')
            ->select('comment.*','goods.*')
            ->where('comment.user_id','=',$user['id'])
            ->get();
        return view('home.comment_list',['user'=>$user,'comment'=>$data]);
    }
    //用户意见反馈
    public function req()
    {
       $user = Session::get('user')[0];
       return view('home.req',['user'=>$user]);
    }
    //添加意见
    public function addreq(Request $request)
    {
        $data['user_id']   = $user = Session::get('user')[0]['id'];
        $data['time']      = time();
        $data['content']   = $request->req;
        $data['status']    = 0;
        DB::table('req')->insert($data);
        return redirect('/home/user');
    }
    //用户回复
    public function reply()
    {
        $user = Session::get('user')[0];
        $data = DB::table('reply')
            ->join('req','req.id','=','reply.req_id')
            ->select('reply.*')
            ->where('req.user_id','=',$user['id'])
            ->get();
        //dd($data);
        return view('home.reply',['user'=>$user,'reply'=>$data]);
    }
  
}
